package springapp.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import springapp.entity.ReviewEntity;

@Repository
public class ReviewDao extends HibernateDao<ReviewEntity, Integer> {

	public static final String[] COLUMNS = new String[] { "review", "rating",
			"gender", "experience", "locationId", "jobType", "designationId" };

	/**
	 * Can't really set criteria for companyId
	 * @param companyId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ReviewEntity> getReviewsForCompanyId(Integer companyId) {
		Map<String, Object> searhMap = new HashMap<String, Object>();
		searhMap.put("companyEntity.companyId", companyId);
		List<Object[]> list = executeSqlQuery(COLUMNS, "ReviewEntity", searhMap);
		List<ReviewEntity> reviewEntities = new ArrayList<ReviewEntity>();
		for (Object[] objects : list) {
			ReviewEntity reviewEntity = new ReviewEntity();
			reviewEntity.setReview((String) objects[0]);
			reviewEntity.setRating((Integer) objects[1]);
			reviewEntity.setGender((String) objects[2]);
			reviewEntity.setExperience((Integer) objects[3]);
			reviewEntity.setLocationId((Integer) objects[4]);
			reviewEntity.setJobType((String) objects[5]);
			reviewEntity.setDesignationId((Integer) objects[6]);
			reviewEntities.add(reviewEntity);
		}
		return reviewEntities;
	}
	
	@SuppressWarnings("rawtypes")
	public int getNumReviewForCompany(Integer companyId){
		String sql = "select count(*) from ReviewEntity where companyEntity.companyId="+companyId;
		List list = executeAggregateQuery(sql);
		return (Integer) list.get(0);
	}
	
}
